package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setStatus(StatusConstant.DISABLE);

        //向套餐表插入数据
        setmealMapper.insert(setmeal);
        //获取生成的套餐id
        Long id = setmeal.getId();

        //获取套餐与菜品的关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
        });

        //保存套餐和菜品的关系
        setmealDishMapper.insertBatch(setmealDishes);

    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {

        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();

        return new PageResult(total, result);
    }

    /**
     * 根据ids删除套餐
     *
     * @param ids
     */
    @Override
    public void delete(Long[] ids) {

        for (Long id : ids) {
            Integer status = setmealMapper.getById(id).getStatus();
            if(status == 1){
                throw new SetmealEnableFailedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //根据ids删除setmael中的套餐数据
        setmealMapper.deleteByIds(ids);

        //根据ids删除setmeal_dish中的数据
        setmealDishMapper.deleteBysetmealIds(ids);


    }

    /**
     * 根据套餐id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO queryById(long id) {
        //根据id返回setmeal表中数据，并加入category表中category.name
        SetmealVO setmealVo = setmealMapper.selectByid(id);

        //根据id查询setmael_dish表中数据，并返回Array<SetmealDish>数组
        ArrayList<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);

        //将数据封装近Setmeal对象中并返回
        setmealVo.setSetmealDishes(setmealDishes);

        return setmealVo;
    }

    /**
     * 根据数据修改套餐
     *
     * @param setmealDTO
     */
    public void update(SetmealDTO setmealDTO) {
        //修改setment数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        Long id = setmealDTO.getId();
        //先根据id删除setmeal_dish数据,再添加
        setmealDishMapper.deleteBysetmealIds(new Long[]{id});

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //如果setmealDishes不为空，添加数据
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(id);
            });
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 修改套餐状态
     * @param status
     */
    public void stopOrStar(Integer status,Long id) {
        log.info("修改套餐id:{}，状态为:{}", id, status==1?"启用":"停用");

        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
